const express = require("express");
const router = express.Router();
const configController = require("../../controllers/configController");
const { validateRequest } = require("../../middleware/validationMiddleware");
const { body, param, query } = require("express-validator");

// 创建系统配置
router.post(
  "/config",
  [
    body("config_name").notEmpty().withMessage("配置名称不能为空"),
    body("config_key").notEmpty().withMessage("配置key不能为空"),
    body("config_value").notEmpty().withMessage("配置值不能为空"),
    body("create_by").notEmpty().withMessage("create_by不能为空222"),
  ],
  validateRequest,
  configController.createConfig
);

// 更新系统配置
router.put(
  "/config/:id",
  [
    param("id").isInt().withMessage("无效的配置ID"),
    body("config_name").optional().notEmpty().withMessage("配置名称不能为空"),
    body("config_key").optional().notEmpty().withMessage("配置key不能为空"),
    body("config_value").optional().notEmpty().withMessage("配置值不能为空"),
    body("update_by").notEmpty().withMessage("update_by不能为空"),
  ],
  validateRequest,
  configController.updateConfig
);

// 删除系统配置
router.delete(
  "/config/:id",
  [param("id").isInt().withMessage("无效的配置ID")],
  validateRequest,
  configController.deleteConfig
);

// 获取系统配置分页列表
router.get(
  "/config/page",
  [
    query("pageNum")
      .exists()
      .withMessage("pageNum参数缺失")
      .isInt({ min: 1 })
      .withMessage("pageNum必须为大于0的整数"),
    query("pageSize")
      .exists()
      .withMessage("pageSize参数缺失")
      .isInt({ min: 1, max: 100 })
      .withMessage("pageSize必须为1-100之间的整数"),
    query("config_name").optional().isString(),
    query("config_key").optional().isString(),
  ],
  validateRequest,
  configController.getConfigList
);

// 获取系统配置详情
router.get(
  "/config/:id",
  [param("id").isInt().withMessage("无效的配置ID")],
  validateRequest,
  configController.getConfigById
);

// 获取所有系统配置
router.get(
  "/config",
  [
    query("config_name")
      .optional()
      .isString()
      .withMessage("配置名称必须为字符串"),
    query("config_key")
      .optional()
      .isString()
      .withMessage("配置key必须为字符串"),
  ],
  validateRequest,
  configController.getAllConfigs
);

module.exports = router;
